package com.example.springbootdemo.handler;

import com.example.springbootdemo.exception.BusinessErrorException;
import com.example.springbootdemo.exception.BusinessResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.web.session.InvalidSessionStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录超时的处理
 * 用户登录后，当达到超时时间后（session过期），自动将用户退出登录
 * @author hh
 * @date 2022/1/28
 */
@Slf4j
@Component
public class InvalidSessionHandler implements InvalidSessionStrategy {

    @Override
    public void onInvalidSessionDetected(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        log.info("用户登录超时，访问[{}]失败", request.getRequestURI());
        throw new BusinessErrorException(BusinessResponseEnum.USER_LOGIN_TIMEOUT);
    }

}
